<?php

namespace app\admin\controller;

use app\admin\model\Admin;
use app\admin\model\User;
use app\admin\model\UserStatistics;
use app\admin\model\DistributeStatistics;
use app\admin\model\DistributeTask;
use app\common\controller\Backend;
use app\common\model\Attachment;
use app\admin\model\EventStatisticsPage;
use fast\Date;
use think\Db;

/**
 * 控制台
 *
 * @icon   fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{

    /**
     * 查看
     */
    public function index()
    {
        try {
            \think\Db::execute("SET @@sql_mode='';");
        } catch (\Exception $e) {

        }

        $params = $this->request->post();
        //dd($params);
        $column = [];
        $starttime = Date::unixtime('day', -6);
        $endtime = Date::unixtime('day', 0, 'end');
        $joinlist = Db("user")->where('add_time', 'between time', [$starttime, $endtime])
            ->where(['virtual_status'=>['neq',2]])
            ->field('add_time, status, COUNT(*) AS nums, DATE_FORMAT(FROM_UNIXTIME(add_time), "%Y-%m-%d") AS join_date')
            ->group('join_date')
            ->select();
        for ($time = $starttime; $time <= $endtime;) {
            $column[] = date("Y-m-d", $time);
            $time += 86400;
        }
        $userlist = array_fill_keys($column, 0);
        foreach ($joinlist as $k => $v) {
            $userlist[$v['join_date']] = $v['nums'];
        }

        $dbTableList = Db::query("SHOW TABLE STATUS");
        $addonList = get_addon_list();
        $totalworkingaddon = 0;
        $totaladdon = count($addonList);
        foreach ($addonList as $index => $item) {
            if ($item['state']) {
                $totalworkingaddon += 1;
            }
        }
        //用户访问页面总数
        $EventStatisticsPageSum = EventStatisticsPage::sum('sum');
        //用户平均访问页面数量
        $EventStatisticsPageCount = ceil($EventStatisticsPageSum/EventStatisticsPage::group('user_id')->count());


        if(empty($params['start_date'])){
            // $statistics_sum1 = DistributeTask::whereTime('receive_time', 'today')->sum('statistics_num');
            // $statistics_sum2 = DistributeStatistics::whereTime('add_time', 'today')->sum('num');
            // 'statistics_sum'    => $statistics_sum1 + $statistics_sum2,
            // 'statistics_wc_sum' => DistributeTask::whereTime('receive_time', 'today')->where(['status'=>3])->count(),
            // 'statistics_commission' => DistributeTask::whereTime('receive_time', 'today')->where(['status'=>3])->sum('price'),

            $this->view->assign([
                'task_1'=>DistributeTask::whereTime('receive_time', 'today')->where(['type'=>'余享小红书'])->sum('statistics_num'),
                'task_2'=>DistributeTask::whereTime('receive_time', '-7 days')->where(['type'=>'余享小红书'])->sum('statistics_num'),
                'task_3'=>DistributeTask::whereTime('receive_time', '-30 days')->where(['type'=>'余享小红书'])->sum('statistics_num'),
                'task_4'=>DistributeTask::whereTime('receive_time', 'today')->where(['type'=>'余享小红书'])->where(['status'=>3])->count(),
                'task_5'=>DistributeTask::whereTime('receive_time', '-7 days')->where(['type'=>'余享小红书'])->where(['status'=>3])->count(),
                'task_6'=>DistributeTask::whereTime('receive_time', '-30 days')->where(['type'=>'余享小红书'])->where(['status'=>3])->count(),
                'task_7'=>DistributeTask::whereTime('receive_time', 'today')->where(['type'=>'余享小红书'])->where(['status'=>3])->sum('price'),
                'task_8'=>DistributeTask::whereTime('receive_time', '-7 days')->where(['type'=>'余享小红书'])->where(['status'=>3])->sum('price'),
                'task_9'=>DistributeTask::whereTime('receive_time', '-30 days')->where(['type'=>'余享小红书'])->where(['status'=>3])->sum('price'),
            ]);

            $this->view->assign([
                'username'          => $this->auth->username,
                'totaluser'         => User::count(),
                'totaladdon'        => $totaladdon,
                'EventStatisticsPageSum'        => $EventStatisticsPageSum,
                'EventStatisticsPageCount'        => $EventStatisticsPageCount,
                'totaladmin'        => Admin::count(),
                'totalcategory'     => \app\common\model\Category::count(),
                'todayusersignup'   => User::whereTime('add_time', 'today')->where(['virtual_status'=>['neq',2]])->count(),
                'threednu'          => User::whereTime('add_time', '-7 days')->where(['virtual_status'=>['neq',2]])->count(),
                'sevendnu'          => User::whereTime('add_time', '-30 days')->where(['virtual_status'=>['neq',2]])->count(),
                'todayuserlogin'    => User::whereTime('last_login_time', 'today')->where(['virtual_status'=>['neq',2]])->count(),
                'sevendau'          => User::whereTime('last_login_time', '-7 days')->where(['virtual_status'=>['neq',2]])->count(),
                'thirtydau'         => User::whereTime('last_login_time', '-30 days')->where(['virtual_status'=>['neq',2]])->count(),
                'todayhuoyue'       => UserStatistics::whereTime('add_time', 'today')->count(),
                'sevendauhuoyue'    => UserStatistics::whereTime('add_time', '-7 days')->count(),
                'thirtydauhuoyue'   => UserStatistics::whereTime('add_time', '-30 days')->count(),
                'dbtablenums'       => count($dbTableList),
                'dbsize'            => array_sum(array_map(function ($item) {
                    return $item['Data_length'] + $item['Index_length'];
                }, $dbTableList)),
                'totalworkingaddon' => $totalworkingaddon,
                'attachmentnums'    => Attachment::count(),
                'attachmentsize'    => Attachment::sum('filesize'),
                'picturenums'       => Attachment::where('mimetype', 'like', 'image/%')->count(),
                'picturesize'       => Attachment::where('mimetype', 'like', 'image/%')->sum('filesize'),
            ]);
        }else{
            $time1 = strtotime($params['start_date']);
            $time2 = $time1 + 86400;
            $time7 = $time1 - (86400*6);
            $time30 = $time1 - (86400*29);
            $where = ['add_time' => ['between', [$time1, $time2]]];
            $where7 = ['add_time' => ['between', [$time7, $time2]]];
            $where30 = ['add_time' => ['between', [$time30, $time2]]];
            $where_last = ['last_login_time' => ['between', [$time1, $time2]]];
            $where_last7 = ['last_login_time' => ['between', [$time7, $time2]]];
            $where_last30 = ['last_login_time' => ['between', [$time30, $time2]]];

            $sta_where1 = ['receive_time'=>['between', [$time1, $time2]]];
            $sta_where7 = ['receive_time'=>['between', [$time7, $time2]]];
            $sta_where30 = ['receive_time'=>['between', [$time30, $time2]]];
            $sta_where2 = ['add_time'=>['between', [$time1, $time2]]];
            //$statistics_sum1 = DistributeTask::where($sta_where1)->sum('statistics_num');
            //$statistics_sum2 = DistributeStatistics::where($sta_where2)->sum('num');

            $this->view->assign([
                'task_1'=>DistributeTask::where($sta_where1)->where(['type'=>'余享小红书'])->sum('statistics_num'),
                'task_2'=>DistributeTask::where($sta_where7)->where(['type'=>'余享小红书'])->sum('statistics_num'),
                'task_3'=>DistributeTask::where($sta_where30)->where(['type'=>'余享小红书'])->sum('statistics_num'),
                'task_4'=>DistributeTask::where($sta_where1)->where(['type'=>'余享小红书'])->where(['status'=>3])->count(),
                'task_5'=>DistributeTask::where($sta_where7)->where(['type'=>'余享小红书'])->where(['status'=>3])->count(),
                'task_6'=>DistributeTask::where($sta_where30)->where(['type'=>'余享小红书'])->where(['status'=>3])->count(),
                'task_7'=>DistributeTask::where($sta_where1)->where(['type'=>'余享小红书'])->where(['status'=>3])->sum('price'),
                'task_8'=>DistributeTask::where($sta_where7)->where(['type'=>'余享小红书'])->where(['status'=>3])->sum('price'),
                'task_9'=>DistributeTask::where($sta_where30)->where(['type'=>'余享小红书'])->where(['status'=>3])->sum('price'),
            ]);


            $this->view->assign([
                'username'          => $this->auth->username,
                'totaluser'         => User::count(),
                'totaladdon'        => $totaladdon,
                'totaladmin'        => Admin::count(),
                'EventStatisticsPageSum'        => $EventStatisticsPageSum,
                'EventStatisticsPageCount'        => $EventStatisticsPageCount,
                'totalcategory'     => \app\common\model\Category::count(),
                'todayusersignup'   => User::where($where)->where(['virtual_status'=>['neq',2]])->count(),
                'threednu'          => User::where($where7)->where(['virtual_status'=>['neq',2]])->count(),
                'sevendnu'          => User::where($where30)->where(['virtual_status'=>['neq',2]])->count(),
                'todayuserlogin'    => User::where($where_last)->where(['virtual_status'=>['neq',2]])->count(),
                'sevendau'          => User::where($where_last7)->where(['virtual_status'=>['neq',2]])->count(),
                'thirtydau'         => User::where($where_last30)->where(['virtual_status'=>['neq',2]])->count(),
                'todayhuoyue'       => UserStatistics::where($where)->count(),
                'sevendauhuoyue'    => UserStatistics::where($where7)->count(),
                'thirtydauhuoyue'   => UserStatistics::where($where30)->count(),
                //'statistics_sum'    => $statistics_sum1 + $statistics_sum2,
                //'statistics_wc_sum' => DistributeTask::where($sta_where1)->where(['status'=>3])->count(),
                //'statistics_commission' => DistributeTask::where($sta_where1)->where(['status'=>3])->sum('price'),
                'dbtablenums'       => count($dbTableList),
                'dbsize'            => array_sum(array_map(function ($item) {
                    return $item['Data_length'] + $item['Index_length'];
                }, $dbTableList)),
                'totalworkingaddon' => $totalworkingaddon,
                'attachmentnums'    => Attachment::count(),
                'attachmentsize'    => Attachment::sum('filesize'),
                'picturenums'       => Attachment::where('mimetype', 'like', 'image/%')->count(),
                'picturesize'       => Attachment::where('mimetype', 'like', 'image/%')->sum('filesize'),
            ]);
            
        }
    
        $this->assignconfig('column', array_keys($userlist));
        $this->assignconfig('userdata', array_values($userlist));
        //查询每日时间点的浏览量
        $task_time_arr = [];
        if(!empty($params['start_date'])){
            $time = strtotime($params['start_date']);
            for ($i=0; $i < 12; $i++) {
                $a = $i*2;
                $b = ($i*2)+2;
                $k = $a.'~'.$b.'时'; 
                $time1 = $time + ($i*2*3600);
                $time2 = $time + (($i*2+2)*3600);
                $where = [
                    'status'=>3,
                    'receive_time'=>['between', [$time1, $time2]]
                ];
                $task_time_arr[$k] = DistributeTask::where($where)->sum('statistics_num');
            }
        }
        $this->assignconfig('task_column', array_keys($task_time_arr));
        $this->assignconfig('task_data', array_values($task_time_arr));
        $this->assignconfig('is_task', $task_time_arr ? 1:0);
        $this->view->assign('is_task',$task_time_arr ? 1:0);
        return $this->view->fetch();
    }

    //获取详情
    public function get_details()
    {
        $params = $this->request->post();
        $where_xcx = ['login_source'=>'xcx','virtual_status'=>['neq',2]];
        $where_ios = ['login_source'=>'ios','virtual_status'=>['neq',2]];
        $where_android = ['login_source'=>'android','virtual_status'=>['neq',2]];
        $where_new = ['login_source'=>'new','virtual_status'=>['neq',2]];
        if(empty($params['start_date'])){
            if($params['type'] == 1){
                $arr = [
                    'xcx'=>User::whereTime('add_time', 'today')->where($where_xcx)->count(),
                    'ios'=>User::whereTime('add_time', 'today')->where($where_ios)->count(),
                    'android'=>User::whereTime('add_time', 'today')->where($where_android)->count(),
                    'new'=>User::whereTime('add_time', 'today')->where($where_new)->count()
                ];
            }elseif($params['type'] == 2){
                $arr = [
                    'xcx'=>User::whereTime('add_time', '-7 days')->where($where_xcx)->count(),
                    'ios'=>User::whereTime('add_time', '-7 days')->where($where_ios)->count(),
                    'android'=>User::whereTime('add_time', '-7 days')->where($where_android)->count(),
                    'new'=>User::whereTime('add_time', '-7 days')->where($where_new)->count()
                ];
            }elseif($params['type'] == 3){
                $arr = [
                    'xcx'=>User::whereTime('add_time', '-30 days')->where($where_xcx)->count(),
                    'ios'=>User::whereTime('add_time', '-30 days')->where($where_ios)->count(),
                    'android'=>User::whereTime('add_time', '-30 days')->where($where_android)->count(),
                    'new'=>User::whereTime('add_time', '-30 days')->where($where_new)->count()
                ];
            }elseif($params['type'] == 4){
                $arr = [
                    'xcx'=>User::whereTime('last_login_time', 'today')->where($where_xcx)->count(),
                    'ios'=>User::whereTime('last_login_time', 'today')->where($where_ios)->count(),
                    'android'=>User::whereTime('last_login_time', 'today')->where($where_android)->count(),
                    'new'=>User::whereTime('last_login_time', 'today')->where($where_new)->count()
                ];
            }elseif($params['type'] == 5){
                $arr = [
                    'xcx'=>User::whereTime('last_login_time', '-7 days')->where($where_xcx)->count(),
                    'ios'=>User::whereTime('last_login_time', '-7 days')->where($where_ios)->count(),
                    'android'=>User::whereTime('last_login_time', '-7 days')->where($where_android)->count(),
                    'new'=>User::whereTime('last_login_time', '-7 days')->where($where_new)->count()
                ];
            }elseif($params['type'] == 6){
                $arr = [
                    'xcx'=>User::whereTime('last_login_time', '-30 days')->where($where_xcx)->count(),
                    'ios'=>User::whereTime('last_login_time', '-30 days')->where($where_ios)->count(),
                    'android'=>User::whereTime('last_login_time', '-30 days')->where($where_android)->count(),
                    'new'=>User::whereTime('last_login_time', '-30 days')->where($where_new)->count()
                ];
            }elseif($params['type'] == 7){
                $where_xcx = ['type'=>'小程序'];
                $where_ios = ['type'=>'苹果'];
                $where_android = ['type'=>'安卓'];
                $arr = [
                    'xcx'=>UserStatistics::whereTime('add_time', 'today')->where($where_xcx)->count(),
                    'ios'=>UserStatistics::whereTime('add_time', 'today')->where($where_ios)->count(),
                    'android'=>UserStatistics::whereTime('add_time', 'today')->where($where_android)->count(),
                ];
            }elseif($params['type'] == 8){
                $where_xcx = ['type'=>'小程序'];
                $where_ios = ['type'=>'苹果'];
                $where_android = ['type'=>'安卓'];
                $arr = [
                    'xcx'=>UserStatistics::whereTime('add_time', '-7 days')->where($where_xcx)->count(),
                    'ios'=>UserStatistics::whereTime('add_time', '-7 days')->where($where_ios)->count(),
                    'android'=>UserStatistics::whereTime('add_time', '-7 days')->where($where_android)->count(),
                ];
            }elseif($params['type'] == 9){
                $where_xcx = ['type'=>'小程序'];
                $where_ios = ['type'=>'苹果'];
                $where_android = ['type'=>'安卓'];
                $arr = [
                    'xcx'=>UserStatistics::whereTime('add_time', '-30 days')->where($where_xcx)->count(),
                    'ios'=>UserStatistics::whereTime('add_time', '-30 days')->where($where_ios)->count(),
                    'android'=>UserStatistics::whereTime('add_time', '-30 days')->where($where_android)->count(),
                ];
            }
        }else{
            $time1 = strtotime($params['start_date']);
            $time2 = $time1 + 86400;
            $time7 = $time1 - (86400*6);
            $time30 = $time1 - (86400*29);
            $where = ['add_time' => ['between', [$time1, $time2]]];
            $where7 = ['add_time' => ['between', [$time7, $time2]]];
            $where30 = ['add_time' => ['between', [$time30, $time2]]];
            $where_last = ['last_login_time' => ['between', [$time1, $time2]]];
            $where_last7 = ['last_login_time' => ['between', [$time7, $time2]]];
            $where_last30 = ['last_login_time' => ['between', [$time30, $time2]]];

            if($params['type'] == 1){
                $arr = [
                    'xcx'=>User::where($where)->where($where_xcx)->count(),
                    'ios'=>User::where($where)->where($where_ios)->count(),
                    'android'=>User::where($where)->where($where_android)->count(),
                    'new'=>User::where($where)->where($where_new)->count()
                ];
            }elseif($params['type'] == 2){
                $arr = [
                    'xcx'=>User::where($where7)->where($where_xcx)->count(),
                    'ios'=>User::where($where7)->where($where_ios)->count(),
                    'android'=>User::where($where7)->where($where_android)->count(),
                    'new'=>User::where($where7)->where($where_new)->count()
                ];
            }elseif($params['type'] == 3){
                $arr = [
                    'xcx'=>User::where($where30)->where($where_xcx)->count(),
                    'ios'=>User::where($where30)->where($where_ios)->count(),
                    'android'=>User::where($where30)->where($where_android)->count(),
                    'new'=>User::where($where30)->where($where_new)->count()
                ];
            }elseif($params['type'] == 4){
                $arr = [
                    'xcx'=>User::where($where_last)->where($where_xcx)->count(),
                    'ios'=>User::where($where_last)->where($where_ios)->count(),
                    'android'=>User::where($where_last)->where($where_android)->count(),
                    'new'=>User::where($where_last)->where($where_new)->count()
                ];
            }elseif($params['type'] == 5){
                $arr = [
                    'xcx'=>User::where($where_last7)->where($where_xcx)->count(),
                    'ios'=>User::where($where_last7)->where($where_ios)->count(),
                    'android'=>User::where($where_last7)->where($where_android)->count(),
                    'new'=>User::where($where_last7)->where($where_new)->count()
                ];
            }elseif($params['type'] == 6){
                $arr = [
                    'xcx'=>User::where($where_last30)->where($where_xcx)->count(),
                    'ios'=>User::where($where_last30)->where($where_ios)->count(),
                    'android'=>User::where($where_last30)->where($where_android)->count(),
                    'new'=>User::where($where_last30)->where($where_new)->count()
                ];
            }elseif($params['type'] == 7){
                $where_xcx = ['type'=>'小程序'];
                $where_ios = ['type'=>'苹果'];
                $where_android = ['type'=>'安卓'];
                $arr = [
                    'xcx'=>UserStatistics::where($where)->where($where_xcx)->count(),
                    'ios'=>UserStatistics::where($where)->where($where_ios)->count(),
                    'android'=>UserStatistics::where($where)->where($where_android)->count(),
                ];
            }elseif($params['type'] == 8){
                $where_xcx = ['type'=>'小程序'];
                $where_ios = ['type'=>'苹果'];
                $where_android = ['type'=>'安卓'];
                $arr = [
                    'xcx'=>UserStatistics::where($where7)->where($where_xcx)->count(),
                    'ios'=>UserStatistics::where($where7)->where($where_ios)->count(),
                    'android'=>UserStatistics::where($where7)->where($where_android)->count(),
                ];
            }elseif($params['type'] == 9){
                $where_xcx = ['type'=>'小程序'];
                $where_ios = ['type'=>'苹果'];
                $where_android = ['type'=>'安卓'];
                $arr = [
                    'xcx'=>UserStatistics::where($where30)->where($where_xcx)->count(),
                    'ios'=>UserStatistics::where($where30)->where($where_ios)->count(),
                    'android'=>UserStatistics::where($where30)->where($where_android)->count(),
                ];
            }
        }
        if(!empty($arr['new'])){
            $html = '<div>小程序用户'.$arr['xcx'].'个</div><div>安卓用户'.$arr['android'].'个</div><div>苹果用户'.$arr['ios'].'个</div><div>拉新用户'.$arr['new'].'个</div>';
        }else{
            $html = '<div>小程序用户'.$arr['xcx'].'个</div><div>安卓用户'.$arr['android'].'个</div><div>苹果用户'.$arr['ios'].'个</div>';
        }
        return json($html);
       
    }

    //两个日期之间相差的天数
    function diffBetweenTwoDays ($day1, $day2)
    {
        $second1 = strtotime($day1);
        $second2 = strtotime($day2);

        // if ($second1 < $second2) {
        //     $tmp = $second2;
        //     $second2 = $second1;
        //     $second1 = $tmp;
        // }
        return ($second1 - $second2) / 86400;
    }

}
